﻿package boyan.douban;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 
 * @author haipeng.liang <br>
 *         file: com.zhibo.xmt.common.util.mangeMaterial.ExcelUtils<br>
 *         time: 2017年10月31日下午6:09:43<br>
 *         作用:excel
 */
public class ExcelUtils {



	public static void exportExcel(List<String> head, List<List<String>> body, OutputStream fos)
			throws IOException {
		//建立工作簿
		Workbook wb = new HSSFWorkbook();
		//建立样式类
		CellStyle cellStyle = wb.createCellStyle();
		//字体类
		Font font = wb.createFont();
		//设为粗体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		cellStyle.setFont(font);

		//建立一个sheet
		Sheet createSheet = wb.createSheet();

		if (head != null && head.size() > 0) {
			//创建一个行
			Row createRowt = createSheet.createRow(0);
			for (int i = 0; i < head.size(); i++) {
				//创建一个行内的一个格子
				Cell cell = createRowt.createCell(i);
				//设置样式
				cell.setCellValue(head.get(i));
				cell.setCellStyle(cellStyle);
			}
		}

		if (body != null && body.size() > 0) {
			Row createRowt = null;
			for (int i = 0; i < body.size(); i++) {
				List<String> row = body.get(i);
				createRowt = createSheet.createRow(i+1);
				for (int j = 0; j < row.size(); j++) {
					Cell cell = createRowt.createCell(j);
					cell.setCellValue(row.get(j));
				}
			}
		}
		wb.write(fos);
	}
}
